/**
 * featurewidget_base 模块.
 */

var FG;
if (!FG)
    FG = {};


FG.FeatureWidgetBase = L.Popup.extend({
    includes: [L.Mixin.Events ],

    options: {
        minWidth: 50,
        maxWidth: 1000,
        // maxHeight: null,
        autoPan: true,
        closeButton: false,
        offset: [0, 7],
        autoPanPadding: [45, 10],
        //autoPanPadding: [100, 100],
        autoPanPaddingTopLeft: [45, 10],
        autoPanPaddingBottomRight: [45, 45],
        keepInView: false,
        className: '',
        zoomAnimation: true,
        widgetClassName: '',
        widgetTemplate: '',  // Override this!
        widgetPrefix: 'leaflet-featurewidget'  // Override this
    },
    
    className: 'FeatureWidgetBase',

    _initLayout: function () {
        var prefix = this.options.widgetPrefix,
            containerClass = prefix + ' ' + this.options.className + ' leaflet-zoom-' +
                (this._animated ? 'animated' : 'hide'),
            container = this._container = L.DomUtil.create('div', containerClass),
            closeButton;

        if (this.options.closeButton) {
            closeButton = this._closeButton =
                L.DomUtil.create('a', prefix + '-close-button', container);
            closeButton.href = '#close';
            closeButton.innerHTML = '&#215;';
            L.DomEvent.disableClickPropagation(closeButton);

            L.DomEvent.on(closeButton, 'click', this._onCloseButtonClick, this);
        }

        var wrapper = this._wrapper =
            L.DomUtil.create('div', prefix + '-content-wrapper', container);
        L.DomEvent.disableClickPropagation(wrapper);

        this._contentNode = L.DomUtil.create('div', prefix + '-content', wrapper);

        L.DomEvent.disableScrollPropagation(this._contentNode);
        L.DomEvent.on(wrapper, 'contextmenu', L.DomEvent.stopPropagation);

        this._tipContainer = L.DomUtil.create('div', prefix + '-tip-container', container);
        this._tip = L.DomUtil.create('div', prefix + '-tip', this._tipContainer);
    },

    renderModel: function(model) {
        this._renderModelBasic(model);

        // 应用到自身
        this.setContent(this.$content[0]);
        return this;
    },

    _renderModelBasic: function(model) {
        var this_ = this;
        this.model = model;
        var uuid = model.get('uuid');
        
        if (!this.$content) {
            var content = $('<div />', {'class': this.options.widgetClassName}).html(this.options.widgetTemplate(model.attributes));
    
            this.$content = content;
            this.$content.attr('uuid', uuid);
            this.$content.attr('tag', 'widget-' + uuid);
        }
        return this;
    }
});

